import { Button, Color, easing, Label, ProgressBar, Slider, Sprite, SpriteFrame, Tween, tween, v3, Vec3 } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';

import Config from '../../../utils/Config';
import DataMgr from '../../../utils/DataMgr';
import { GGStore } from '../../../framework/core/base/GGStore';
import Design from '../../../utils/Design';
import { GGResource } from '../../../framework/core/base/GGResource';
import { EVENT_NAMES } from '../../../utils/Enums';

const { ccclass, property } = _decorator;

@ccclass('MenuPanel')
export class MenuPanel extends Component {
    
    @property({ type: Label, displayName: "倒计时标签" })
    countdownLabel: Label;
    
    @property({ type: Label, displayName: "当前关卡标签" })
    levelLabel: Label;
    
    @property({ type: ProgressBar, displayName: "倒计时进度" })
    countdownProgress: ProgressBar;

    protected onLoad(): void {
        this.initListener()
    }

    protected onDestroy(): void {
        gg.systemEvent.off(EVENT_NAMES.UPDATE_GAME_INFO, this.panelUpdate, this)
        gg.systemEvent.off(EVENT_NAMES.ON_GAME_START, this.onGameStart, this)
    }

    // --------------------- 初始化 --------------------- //
    private initListener() {
        gg.systemEvent.on(EVENT_NAMES.UPDATE_GAME_INFO, this.panelUpdate, this)
        gg.systemEvent.on(EVENT_NAMES.ON_GAME_START, this.onGameStart, this)
    }

    // --------------------- 事件 --------------------- //
    // 倒计时
    private panelUpdate(info: object) {
        const timeFloat = parseFloat(info["time"])
        
        this.countdownProgress.progress = timeFloat / DataMgr.countDown
        this.countdownLabel.string = Config.formatCountdown(timeFloat)
    }

    private onSettingClicked() {
        AudioManager.playButton()
        UIManager.open("setting:res/prefab/SettingPanel")
    }

    private onGameStart() {
        this.levelLabel.string = DataMgr.currentLevel + ""
    }
}
